package com.wuxiaoqin.admin.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {


    /**
     * 创建多个分组
     * @return
     */
//    @Bean
//    public Docket docket1() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName("A");
//    }
//    @Bean
//    public Docket docket2() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName("B");
//    }
//    @Bean
//    public Docket docket3() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName("C");
//    }
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //设置API接口文档分组
                .groupName("杨叶帆")
                //.enable(false)关闭swagger
                .select()
                //指定要扫描的包
                //any() none()
                //withClassAnnotation(GetMapping.class) //扫描类上的注解
                //withMethodAnnotation(  ) //扫描方法上的注解
                //.apis(RequestHandlerSelectors.basePackage("com.wuxiaoqin.admin.controller"))
                .apis(RequestHandlerSelectors.basePackage("com.yyf.admin.controller"))
                //过滤路径
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    @Bean
    public Docket createRestApi() {



        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //设置API接口文档分组
                .groupName("吴晓钦")
                //.enable(false)关闭swagger
                .select()
                //指定要扫描的包
                //any() none()
                //withClassAnnotation(GetMapping.class) //扫描类上的注解
                //withMethodAnnotation(  ) //扫描方法上的注解
               .apis(RequestHandlerSelectors.basePackage("com.wuxiaoqin.admin.controller"))
                //.apis(RequestHandlerSelectors.basePackage("com.*.admin.controller"))
                //过滤路径
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("wuxiaoqin", "http:localhost:8181/doc.html", "2052256855@qq.com");
        return new ApiInfoBuilder()
                .title("服装商城后台系统的接口文档")
                .description("更多请咨询服务开发者吴晓钦和杨叶帆。")
                .contact(contact)
                .version("1.0")
                .build();

    }

    //设置请求头信息
    private List<ApiKey> securitySchemes(){
        List<ApiKey> result= new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization","Authorization","Header");
        result.add(apiKey);
        return result;
    }

    //设置需要登录认证的路径
    private List<SecurityContext> securityContexts(){
        List<SecurityContext> result = new ArrayList<>();
        //result.add(getContextByPath("/hello/.*"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
                return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization",authorizationScopes));
        return result;
    }



}
